package com.facebook.drawee.controller;

import android.graphics.drawable.Animatable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class ForwardingControllerListener<INFO> implements ControllerListener<INFO> {
    private final List<ControllerListener<? super INFO>> mListeners;

    public ForwardingControllerListener() {
        this.mListeners = new ArrayList(2);
    }

    public final void clearListeners() {
        synchronized (this) {
            this.mListeners.clear();
        }
    }

    public synchronized final void addListener(ControllerListener<? super INFO> controllerListener) {
        this.mListeners.add(controllerListener);
    }

    @Override
    public final void onRelease(String id) {
        int size = this.mListeners.size();
        for (int i = 0; i < size; i++) {
            try {
                ControllerListener<? super INFO> controllerListener = this.mListeners.get(i);
                if (controllerListener != null) {
                    controllerListener.onRelease(id);
                }
            } catch (Exception exception) {
                this.onException("InternalListener exception in onRelease", ((Throwable) exception));
            }
        }
    }

    public final synchronized void onSubmit(String id, INFO imageInfo) {
        int size = this.mListeners.size();
        for (int i = 0; i < size; i++) {
            try {
                ControllerListener<? super INFO> controllerListener = mListeners.get(i);
                if (controllerListener != null) {
                    controllerListener.onSubmit(id, imageInfo);
                }
            } catch (Exception exception) {
                this.onException("InternalListener exception in onSubmit", exception);
            }
        }
    }

    public final synchronized void onFinalImageSet(String id, @Nullable INFO imageInfo, @Nullable Animatable animatable) {
        int size = this.mListeners.size();
        for (int i = 0; i < size; i++) {
            ControllerListener<? super INFO> controllerListener = this.mListeners.get(i);
            try {
                if (controllerListener != null) {
                    controllerListener.onFinalImageSet(id, imageInfo, animatable);
                }
            } catch (Exception exception) {
                this.onException("InternalListener exception in onFinalImageSet", ((Throwable) exception));
            }
        }
    }

    public final void onIntermediateImageFailed(String id, Throwable throwable) {
        int size = this.mListeners.size();
        for (int i = 0; i < size; ++i) {
            ControllerListener<? super INFO> controllerListener = this.mListeners.get(i);
            try {
                controllerListener.onIntermediateImageFailed(id, throwable);
            } catch (Exception exception) {
                this.onException("InternalListener exception in onIntermediateImageFailed", exception);
            }
        }
    }

    public final void onIntermediateImageSet(String id, @Nullable INFO imageInfo) {
        for (int i = 0; i < mListeners.size(); i++) {
            ControllerListener<? super INFO> controllerListener = this.mListeners.get(i);
            try {
                controllerListener.onIntermediateImageSet(id, imageInfo);
            } catch (Exception exception) {
                this.onException("InternalListener exception in onIntermediateImageSet", ((Throwable) exception));
            }
        }
    }

    public final synchronized void onFailure(String id, Throwable throwable) {
        for (int i = 0; i < mListeners.size(); i++) {
            ControllerListener<? super INFO> controllerListener = this.mListeners.get(i);
            try {
                controllerListener.onFailure(id, throwable);
            } catch (Exception exception) {
                this.onException("InternalListener exception in onFailure", ((Throwable) exception));
            }
        }
    }

    private synchronized void onException(String id, Throwable throwable) {
        Log.e("FdingControllerListener", id, throwable);
    }
}

